import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:mmkv/mmkv.dart';
import 'package:provider/provider.dart';
import 'package:url_strategy/url_strategy.dart';

import 'nav/nav_route.dart';
import 'provider/provider_app.dart';
import 'provider/provider_login.dart';
import 'utils/error_handle.dart';
import 'utils/log_utils.dart';
import 'utils/screen_adapter.dart';
import 'ui/home/page_home.dart';

//将main函数转为异步方法，使得可以在方法中使用await
Future<void> main() async {
  //是能够抛出异常
  handleError(() async {
    //允许app运行之前与Flutter Engine 进行通信
    WidgetsFlutterBinding.ensureInitialized();
    SystemChrome.setEnabledSystemUIMode(SystemUiMode.immersiveSticky);
    // 初始化mmkv
    final rootDir = await MMKV.initialize();
    LogUtils.i("mmkv rootDir: ${rootDir}");

    final appInfo = AppInfo();
    final loginProvider = LoginProvider();

    // 加载本地用户信息
    appInfo.loadFromLocal();
    //设置配置URL策略
    setPathUrlStrategy();

    runApp(
        MultiProvider(
          providers: [
            ChangeNotifierProvider.value(value: appInfo),
            ChangeNotifierProvider.value(value: loginProvider),
          ],
          child: const MyApp(),
        )
    );
  });
}
class MyApp extends StatelessWidget {
  const MyApp({super.key});

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    Adapt.initialize(context);
    return MaterialApp(
      title: 'Flutter Demo',
      debugShowCheckedModeBanner: false,
      home: const HomePage(),
      initialRoute: routerHomePage,
      routes: NavRouter().getRouter(context),
    );
  }
}
